<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *设备
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'设备列表'),
	array('op'=>'edit','title'=>'添加设备'),
	array('op'=>'jiebang','title'=>'解绑设备'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['apitype'])){
		$params['apitype like']='%'.trim($_RQ['apitype']).'%';
		$return['params']['apitype']=$_RQ['apitype'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aiagent_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$tips='这里可以添加硬件设备，然后关联绑定商城的商品！';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tips,'edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'设备名称','type'=>'string'),
			array('name'=>'logo','title'=>'LOGO','type'=>'image'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'apitype','title'=>'接口参数值','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'设备标题','type'=>'string'),
			array('name'=>'apitype','width'=>15,'title'=>'接口参数值','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aiagent_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回设备列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'设备名称','type'=>'string'),
			array('name'=>'logo','title'=>'LOGO','type'=>'image'),
			array('name'=>'apitype','title'=>'接口参数值','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写设备名称！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
		'apitype'=>trim($postdata['apitype']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aiagent_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aiagent_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择设备！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aiagent_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aiagent_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='jiebang'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['code'] && !$post['type']){
			exi('请输入【配对码】','error');
		}
		if(!$post['deviceid'] && $post['type']){
			exi('请输入【硬件ID】','error');
		}
		if($post['type']){
			$goodslog=pdo_get('jy_aiagent_goodslog',array('deviceid'=>$post['deviceid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$goodslog['id']){
				exi('【硬件ID】不对，没找到对应的设备！','error');
			}
		}else{
			$goodslog=pdo_get('jy_aiagent_goodslog',array('code'=>$post['code'],'uniacid'=>$_SESSION['uniacid']));
			if(!$goodslog['id']){
				exi('【配对码】不对，没找到对应的设备！','error');
			}
		}
		$this->model('api')->delusergoods($goodslog,1);
		pdo_delete('jy_aiagent_goodslog',array('id'=>$goodslog['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('解绑成功！','','list');
	}
	$return['item']=array('type'=>0);
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'解绑设备',
		'edit'=>array(
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
			array('name'=>'code','title'=>'配对码','type'=>'string','show'=>array('type'=>0)),
			array('name'=>'deviceid','title'=>'硬件ID','type'=>'string','show'=>array('type'=>1)),
		),
	);
	$return['type']=array('配对码','硬件ID');
	exi($return);
}